package pers.cj.pdos.controller.analysis;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.BusinessSet;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.InGoodsAnalysis;
import pers.cj.pdos.excel.inGoodsAnalysis.*;
import pers.cj.pdos.service.*;
import pers.cj.pdos.task.CleanTempExcelTask;
import pers.cj.pdos.utils.DateUtils;
import pers.cj.pdos.utils.FileProperUtil;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.InGoodsGoodsVo;
import pers.cj.pdos.vo.InGoodsVo;
import pers.cj.pdos.vo.UserVo;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Controller
@RequestMapping("/goodsCount")
public class InGoodsAnalysisController extends BaseController {
    @Autowired
    IInGoodsAnalysisService inGoodsAnalysisService;
    @Autowired
    IStoreService storeService;
    @Autowired
    IHouseService houseService;
    @Autowired
    IGoodsService goodsService;
    @Autowired
    IProviderService providerService;
    @Autowired
    IInGoodsService inGoodsService;


    @RequiresPermissions("inGoodsAnalysis:query")
    @RequestMapping("/inGoodsAnalysis")
    public String inGoodsAnalysis(Model model, String date1, String date2) {
        model.addAttribute("date1",date1);
        model.addAttribute("date2",date2);
        return "analysis/inGoods/inGoodsAnalysis";
    }

    @RequiresPermissions("inGoodsAnalysis:query")
    @RequestMapping("/inGoodsAnalysisDetailGoods")
    public String refunAnalysisDetailGoods(Model model, String goodsId,String storeId,String houseId, String date1, String date2) {
        model.addAttribute("goodsId",goodsId);
        model.addAttribute("storeId",storeId);
        model.addAttribute("houseId",houseId);
        model.addAttribute("date1",date1);
        model.addAttribute("date2",date2);

        model.addAttribute("goodsVo",  goodsService.getVoById(goodsId));

        if (!Utils.isEmpty(storeId)) {
            model.addAttribute("storeName",storeService.getById(storeId).getName());
        }

        if (!Utils.isEmpty(houseId)) {
            model.addAttribute("houseName",houseService.getById(houseId).getName());
        }

        return "analysis/inGoods/inGoodsAnalysisDetailGoods";
    }

    @RequiresPermissions("inGoodsAnalysis:query")
    @RequestMapping("/inGoodsAnalysisDetailProvider")
    public String inGoodsAnalysisDetailProvider(Model model, String providerId,String storeId,String houseId, String date1, String date2) {
        model.addAttribute("providerId",providerId);
        model.addAttribute("storeId",storeId);
        model.addAttribute("houseId",houseId);
        model.addAttribute("date1",date1);
        model.addAttribute("date2",date2);

        model.addAttribute("provider",  providerService.getById(providerId));

        if (!Utils.isEmpty(storeId)) {
            model.addAttribute("storeName",storeService.getById(storeId).getName());
        }

        if (!Utils.isEmpty(houseId)) {
            model.addAttribute("houseName",houseService.getById(houseId).getName());
        }

        return "analysis/inGoods/inGoodsAnalysisDetailProvider";
    }

    @RequiresPermissions("inGoodsAnalysis:query")
    @RequestMapping("/inGoodsAnalysisDetailOrder")
    public String inGoodsAnalysisDetailOrder(Model model, String storeId,String houseId, String date) {
        model.addAttribute("date",date);
        model.addAttribute("storeId",storeId);
        model.addAttribute("houseId",houseId);

        if (!Utils.isEmpty(storeId)) {
            model.addAttribute("storeName",storeService.getById(storeId).getName());
        }

        if (!Utils.isEmpty(houseId)) {
            model.addAttribute("houseName",houseService.getById(houseId).getName());
        }

        return "analysis/inGoods/inGoodsAnalysisDetailOrder";
    }


    @ResponseBody
    @RequestMapping("/inGoodsAnalysis/getAllTotalData")
    public ResponseData getAllTotalData(String storeId,
                                        String houseId,
                                        String date1,
                                        String date2) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        Map map = inGoodsAnalysisService.getTotalData(company.getId(), PdosUtils.getViewOtherStoreId(userVo,storeId), houseId, Utils.genDate(date1), Utils.genDate(date2));

        Map highGoods = inGoodsAnalysisService.getHighGoods(company.getId(), PdosUtils.getViewOtherStoreId(userVo,storeId), houseId, Utils.genDate(date1), Utils.genDate(date2));
        Map highProvider = inGoodsAnalysisService.getHighProvider(company.getId(), PdosUtils.getViewOtherStoreId(userVo,storeId), houseId, Utils.genDate(date1), Utils.genDate(date2));

        map.put("highGoods", highGoods);
        map.put("highProvider", highProvider);
        return ResponseData.success(map);
    }


    @ResponseBody
    @RequestMapping("/inGoodsAnalysis/listStoreData")
    public ResponseList listStoreData(String storeId,
                                      String houseId,
                                      String date1,
                                      String date2) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<InGoodsAnalysis> list = inGoodsAnalysisService.listStoreData(company.getId(),
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2));

        return ResponseList.success(list.size(), list);
    }

    @ResponseBody
    @RequestMapping("/inGoodsAnalysis/listHouseData")
    public ResponseList listHouseData(String storeId,
                                      String houseId,
                                      String date1,
                                      String date2) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<InGoodsAnalysis> list = inGoodsAnalysisService.listHouseData(company.getId(),
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2));

        return ResponseList.success(list.size(), list);
    }

    @ResponseBody
    @RequestMapping("/inGoodsAnalysis/listGoodsData")
    public ResponseList listGoodsData(String page,
                                     String limit,
                                     String storeId,
                                     String houseId,
                                      String providerId,
                                     String date1,
                                     String date2,
                                     String typeId,
                                     String filterZero,
                                     String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<InGoodsAnalysis> list = inGoodsAnalysisService.listGoodsData(company.getId(),
                Utils.genInteger(page),
                Utils.genInteger(limit),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                providerId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                typeId,
                Utils.genBoolean(filterZero),
                Utils.genLikeData(search));

        Long count = inGoodsAnalysisService.countGoodsData(company.getId(),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                providerId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                typeId,
                Utils.genBoolean(filterZero),
                Utils.genLikeData(search));

        return ResponseList.success(count, list);
    }

    @ResponseBody
    @RequestMapping("/inGoodsAnalysis/listProviderData")
    public ResponseList listProviderData(String page,
                                      String limit,
                                      String storeId,
                                      String houseId,
                                      String date1,
                                      String date2,
                                      String providerTypeId,
                                      String filterZero,
                                      String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<InGoodsAnalysis> list = inGoodsAnalysisService.listProviderData(company.getId(),
                Utils.genInteger(page),
                Utils.genInteger(limit),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                providerTypeId,
                Utils.genBoolean(filterZero),
                Utils.genLikeData(search));

        Long count = inGoodsAnalysisService.countProviderData(company.getId(),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                providerTypeId,
                Utils.genBoolean(filterZero),
                Utils.genLikeData(search));

        return ResponseList.success(count, list);
    }

    @ResponseBody
    @RequestMapping("/inGoodsAnalysis/listOrderData")
    public ResponseList listOrderData(String page,
                                     String limit,
                                     String storeId,
                                     String houseId,
                                     String date1,
                                     String date2) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<InGoodsAnalysis> list = inGoodsAnalysisService.listOrderData(company.getId(),
                Utils.genInteger(page),
                Utils.genInteger(limit),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2));

        Long count = inGoodsAnalysisService.countOrderData(company.getId(),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2));

        return ResponseList.success(count, list);
    }

    @ResponseBody
    @RequestMapping("/inGoodsAnalysis/getOrderDetail")
    public ResponseList getOrderDetail(int page,
                                int limit,
                                String close,
                                String search,
                                String no,
                                String goodsName,
                                String remark,
                                String provider,
                                String makeTime1,
                                String makeTime2,
                                String storeId,
                                String userId,
                                String date1,
                                String date2,
                                String date,
                                String providerId,
                                String houseId,
                                String makeUserId){
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();

        List<InGoodsVo> list = inGoodsService.listVo(company.getId(),
                page,
                limit,
                Utils.genBoolean(close),
                Utils.genLikeData(search),
                Utils.genLikeData(no),
                Utils.genLikeData(goodsName),
                Utils.genLikeData(remark),
                Utils.genLikeData(provider),
                Utils.genDate(makeTime1),
                Utils.genDate(makeTime2),
                PdosUtils.getViewOtherStoreId(userVo, storeId),
                null,
                userId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genDate(date),
                providerId,
                houseId,
                makeUserId);
        long count = inGoodsService.count(company.getId(),
                Utils.genBoolean(close),
                Utils.genLikeData(search),
                Utils.genLikeData(no),
                Utils.genLikeData(goodsName),
                Utils.genLikeData(remark),
                Utils.genLikeData(provider),
                Utils.genDate(makeTime1),
                Utils.genDate(makeTime2),
                PdosUtils.getViewOtherStoreId(userVo, storeId),
                null,
                userId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genDate(date),
                providerId,
                houseId,
                makeUserId);
        return ResponseList.success(count,list);
    }


    @ResponseBody
    @RequestMapping("/inGoodsAnalysis/listGoodsDetailDataByOrder")
    public ResponseList listGoodsDetailDataByOrder(String goodsId,
                                            String page,
                                            String limit,
                                            String storeId,
                                            String houseId,
                                            String date1,
                                            String date2,
                                            String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<InGoodsAnalysis> list = inGoodsAnalysisService.listGoodsDetailDataByOrder(company.getId(),
                goodsId,
                Utils.genInteger(page),
                Utils.genInteger(limit),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genLikeData(search));

        Long count = inGoodsAnalysisService.countGoodsDetailDataByOrder(company.getId(),
                goodsId,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genLikeData(search));

        return ResponseList.success(count, list);
    }

    @ResponseBody
    @RequestMapping("/inGoodsAnalysis/listGoodsDetailDataByProvider")
    public ResponseList listGoodsDetailDataByProvider(String goodsId,
                                                   String page,
                                                   String limit,
                                                   String storeId,
                                                   String houseId,
                                                   String date1,
                                                   String date2,
                                                   String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<InGoodsAnalysis> list = inGoodsAnalysisService.listGoodsDetailDataByProvider(company.getId(),
                goodsId,
                Utils.genInteger(page),
                Utils.genInteger(limit),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genLikeData(search));

        Long count = inGoodsAnalysisService.countGoodsDetailDataByProvider(company.getId(),
                goodsId,
                storeId,
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genLikeData(search));

        return ResponseList.success(count, list);
    }

    @ResponseBody
    @RequestMapping("/inGoodsAnalysis/chatDetailData")
    public ResponseData chatDetailData(String goodsId,
                                      String providerId,
                                      String storeId,
                                      String houseId,
                                      String date1,
                                      String date2) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();
        if (Utils.isEmpty(date1) || Utils.isEmpty(date2)) {
            //则日期需要根据数据库出来的数据做判断
            Map map = inGoodsAnalysisService.getMaxMinDate(company.getId(), goodsId, providerId, PdosUtils.getViewOtherStoreId(userVo,storeId),houseId);
            if (map == null) {
                //说明数据库无数据
                return ResponseData.success(null);
            }

            date1 = DateUtils.getDate((Date)map.get("minDate"), DateUtils.YYYY_MM_DD);
            date2 = DateUtils.getDate((Date)map.get("maxDate"), DateUtils.YYYY_MM_DD);
        }

        int flag = PdosUtils.calcDateDiffFlag(date1,date2);
        if (flag == -1) {
            return ResponseData.success(null);
        }

        Map<String, Object> chatMap = PdosUtils.calcDateDiffMap(date1,date2, businessSet);
        if (flag == 1) {
            //按照年份获取总进货额
            List<Map> mapList = inGoodsAnalysisService.getTotalByYear(company.getId(),goodsId, providerId,PdosUtils.getViewOtherStoreId(userVo,storeId),houseId, Utils.genDate(date1), Utils.genDate(date2));
            for (Map map : mapList) {

                chatMap.put(map.get("year")+"年", PdosUtils.getPrice((BigDecimal) map.get("amount"),businessSet));
            }
        } else if (flag == 2) {
            //按照月获取总进货额
            List<Map> mapList = inGoodsAnalysisService.getTotalByMonth(company.getId(),goodsId, providerId,PdosUtils.getViewOtherStoreId(userVo,storeId),houseId, Utils.genDate(date1), Utils.genDate(date2));
            for (Map map : mapList) {
                chatMap.put(map.get("month")+"月", PdosUtils.getPrice((BigDecimal) map.get("amount"),businessSet));
            }
        } else if (flag == 3) {
            //按照日获取总进货额
            List<Map> mapList = inGoodsAnalysisService.getTotalByDate(company.getId(),goodsId, providerId,PdosUtils.getViewOtherStoreId(userVo,storeId),houseId, Utils.genDate(date1), Utils.genDate(date2));
            for (Map map : mapList) {
                chatMap.put(map.get("date")+"日", PdosUtils.getPrice((BigDecimal) map.get("amount"),businessSet));
            }
        }

        return ResponseData.success(chatMap);
    }

    @ResponseBody
    @RequestMapping("/inGoodsAnalysis/listWeekChatData")
    public ResponseData listWeekChatData(String day) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();

        Date date2 = DateUtils.genDate(day, DateUtils.YYYY_MM_DD);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date2);
        calendar.add(Calendar.DATE, -6);
        Date date1 = calendar.getTime();

        // 先准备好初始数据
        Map<String, Object> chatMap = new LinkedHashMap<>();
        for (int i=0;i<7;i++) {
            chatMap.put(calendar.get(Calendar.DAY_OF_MONTH)+"日", new BigDecimal(0));
            calendar.add(Calendar.DATE, 1);
        }

        //按照日获取总进货额
        List<Map> mapList = inGoodsAnalysisService.getTotalByDate(company.getId(),null, null,PdosUtils.getViewOtherStoreId(userVo,null),null, date1, date2);
        for (Map map : mapList) {
            chatMap.put(map.get("date")+"日",  (BigDecimal) map.get("amount"));
        }

        return ResponseData.success(chatMap);
    }


    @ResponseBody
    @RequiresPermissions("inGoodsAnalysis:export")
    @RequestMapping("/inGoodsAnalysis/exportGoodsData")
    public ResponseData exportGoodsData(String fileName,
                                        String storeId,
                                        String houseId,
                                        String providerId,
                                        String date1,
                                        String date2,
                                        String typeId,
                                        String filterZero,
                                        String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();
        List<InGoodsAnalysis> list = inGoodsAnalysisService.listGoodsData(company.getId(),
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                providerId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                typeId,
                Utils.genBoolean(filterZero),
                Utils.genLikeData(search));

        List<ExcelInGoodsAnalysisGoods> excelList = new ArrayList<>();
        for (InGoodsAnalysis entity : list) {
            ExcelInGoodsAnalysisGoods excel = new ExcelInGoodsAnalysisGoods();
            excel.setNo(entity.getNo());
            excel.setName(entity.getName());
            excel.setNorms(entity.getNorms());
            excel.setUnitName(entity.getUnitName());
            excel.setNumber(PdosUtils.getNumber(entity.getNumber(), businessSet));
            excel.setAmount(PdosUtils.getPrice(entity.getAmount(), businessSet));
            excel.setAvgAmount(PdosUtils.getPrice(entity.getAvgAmount(), businessSet));
            excel.setTax(PdosUtils.getPrice(entity.getTax(), businessSet));
            excelList.add(excel);
        }


        String excelName = fileName + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";

        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path + excelName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            Sheet sheet1 = new Sheet(1, 0, ExcelInGoodsAnalysisGoods.class);
            sheet1.setSheetName(fileName + "信息");
            writer.write(excelList, sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(excelName, new Date().getTime());
            return ResponseData.success(excelName);
        } catch (IOException e) {
            return ResponseData.fail("导出失败");
        }
    }

    @ResponseBody
    @RequiresPermissions("inGoodsAnalysis:export")
    @RequestMapping("/inGoodsAnalysis/exportProviderData")
    public ResponseData exportProviderData(String fileName,
                                         String storeId,
                                         String houseId,
                                         String date1,
                                         String date2,
                                         String providerTypeId,
                                         String filterZero,
                                         String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();
        List<InGoodsAnalysis> list = inGoodsAnalysisService.listProviderData(company.getId(),
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                providerTypeId,
                Utils.genBoolean(filterZero),
                Utils.genLikeData(search));

        List<ExcelInGoodsAnalysisProvider> excelList = new ArrayList<>();
        for (InGoodsAnalysis entity : list) {
            ExcelInGoodsAnalysisProvider excel = new ExcelInGoodsAnalysisProvider();
            excel.setNo(entity.getNo());
            excel.setName(entity.getName());
            excel.setNumber(PdosUtils.getNumber(entity.getNumber(), businessSet));
            excel.setAmount(PdosUtils.getPrice(entity.getAmount(), businessSet));
            excel.setRatio(PdosUtils.getNumber(entity.getRatio(), businessSet)+"%");
            excel.setTax(PdosUtils.getPrice(entity.getTax(), businessSet));
            excelList.add(excel);
        }

        String excelName = fileName + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";
        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path + excelName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            Sheet sheet1 = new Sheet(1, 0, ExcelInGoodsAnalysisProvider.class);
            sheet1.setSheetName(fileName + "信息");
            writer.write(excelList, sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(excelName, new Date().getTime());
            return ResponseData.success(excelName);
        } catch (IOException e) {
            return ResponseData.fail("导出失败");
        }
    }

    @ResponseBody
    @RequiresPermissions("inGoodsAnalysis:export")
    @RequestMapping("/inGoodsAnalysis/exportOrderData")
    public ResponseData exportOrderData(String fileName,
                                        String storeId,
                                        String houseId,
                                        String date1,
                                        String date2) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();
        List<InGoodsAnalysis> list = inGoodsAnalysisService.listOrderData(company.getId(),
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2));

        List<ExcelInGoodsAnalysisOrder> excelList = new ArrayList<>();
        for (InGoodsAnalysis entity : list) {
            ExcelInGoodsAnalysisOrder excel = new ExcelInGoodsAnalysisOrder();
            excel.setDate(entity.getDate());
            excel.setNumber(PdosUtils.getNumber(entity.getNumber(), businessSet));
            excel.setAmount(PdosUtils.getPrice(entity.getAmount(), businessSet));
            excel.setTax(PdosUtils.getPrice(entity.getTax(), businessSet));
            excelList.add(excel);
        }

        String excelName = fileName + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";
        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path + excelName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            Sheet sheet1 = new Sheet(1, 0, ExcelInGoodsAnalysisOrder.class);
            sheet1.setSheetName(fileName+"信息");
            writer.write(excelList, sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(excelName, new Date().getTime());
            return ResponseData.success(excelName);
        } catch (IOException e) {
            return ResponseData.fail("导出失败");
        }
    }

    @ResponseBody
    @RequiresPermissions("inGoodsAnalysis:export")
    @RequestMapping("/inGoodsAnalysis/exportGoodsDetailDataByOrder")
    public ResponseData exportGoodsDetailDataByOrder(String goodsId,
                                                     String storeId,
                                                     String houseId,
                                                     String date1,
                                                     String date2,
                                                     String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();
        List<InGoodsAnalysis> list = inGoodsAnalysisService.listGoodsDetailDataByOrder(company.getId(),
                goodsId,
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genLikeData(search));

        List<ExcelInGoodsAnalysisGoodsOrder> excelList = new ArrayList<>();
        for (InGoodsAnalysis entity : list) {
            ExcelInGoodsAnalysisGoodsOrder excel = new ExcelInGoodsAnalysisGoodsOrder();
            excel.setDate(entity.getDate());
            excel.setNo(entity.getNo());
            excel.setProviderName(entity.getProviderName());
            excel.setUnitName(entity.getUnitName());
            excel.setNumber(PdosUtils.getNumber(entity.getNumber(), businessSet));
            excel.setAmount(PdosUtils.getPrice(entity.getAmount(), businessSet));
            excel.setAvgAmount(PdosUtils.getPrice(entity.getAmount().divide(entity.getNumber(),4, RoundingMode.HALF_UP), businessSet));
            excel.setTax(PdosUtils.getPrice(entity.getTax(), businessSet));
            excelList.add(excel);
        }

        String fileName = "进货报表报表按商品按单据表" + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";
        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path + fileName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            Sheet sheet1 = new Sheet(1, 0, ExcelInGoodsAnalysisGoodsOrder.class);
            sheet1.setSheetName("进货报表按商品按单据表信息");
            writer.write(excelList, sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(fileName, new Date().getTime());
            return ResponseData.success(fileName);
        } catch (IOException e) {
            return ResponseData.fail("导出失败");
        }
    }

    @ResponseBody
    @RequiresPermissions("inGoodsAnalysis:export")
    @RequestMapping("/inGoodsAnalysis/exportGoodsDetailDataByProvider")
    public ResponseData exportGoodsDetailDataByProvider(String goodsId,
                                                      String storeId,
                                                      String houseId,
                                                      String date1,
                                                      String date2,
                                                      String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();
        List<InGoodsAnalysis> list = inGoodsAnalysisService.listGoodsDetailDataByProvider(company.getId(),
                goodsId,
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genLikeData(search));

        List<ExcelInGoodsAnalysisGoodsProvider> excelList = new ArrayList<>();
        for (InGoodsAnalysis entity : list) {
            ExcelInGoodsAnalysisGoodsProvider excel = new ExcelInGoodsAnalysisGoodsProvider();
            excel.setName(entity.getName());
            excel.setUnitName(entity.getUnitName());
            excel.setNumber(PdosUtils.getNumber(entity.getNumber(), businessSet));
            excel.setAmount(PdosUtils.getPrice(entity.getAmount(), businessSet));
            excel.setAvgAmount(PdosUtils.getPrice(entity.getAmount().divide(entity.getNumber(),4, RoundingMode.HALF_UP), businessSet));
            excel.setTax(PdosUtils.getPrice(entity.getTax(), businessSet));
            excelList.add(excel);
        }

        String fileName = "进货报表按商品按供应商表" + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";
        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path + fileName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            Sheet sheet1 = new Sheet(1, 0, ExcelInGoodsAnalysisGoodsProvider.class);
            sheet1.setSheetName("进货报表按商品按供应商表信息");
            writer.write(excelList, sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(fileName, new Date().getTime());
            return ResponseData.success(fileName);
        } catch (IOException e) {
            return ResponseData.fail("导出失败");
        }
    }

    @ResponseBody
    @RequiresPermissions("inGoodsAnalysis:export")
    @RequestMapping("/inGoodsAnalysis/exportProviderDetailDataByOrder")
    public ResponseData exportProviderDetailDataByOrder(String providerId,
                                                      String storeId,
                                                      String houseId,
                                                      String date1,
                                                      String date2,
                                                      String search) {
        Company company = getCompany();
        BusinessSet businessSet = getBusinessSet();
        UserVo userVo = getLoginUserVo();
        List<InGoodsVo> list = inGoodsService.listVo(company.getId(),
                null,
                null,
                false,
                Utils.genLikeData(search),
                null,
                null,
                null,
                null,
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                null,
                null,
                Utils.genDate(date1),
                Utils.genDate(date2),
                null,
                providerId,
                houseId,
                null);

        List<ExcelInGoodsAnalysisProviderOrder> excelList = new ArrayList<>();
        for (InGoodsVo entity : list) {
            ExcelInGoodsAnalysisProviderOrder excel = new ExcelInGoodsAnalysisProviderOrder();
            excel.setNo(entity.getNo());
            excel.setDate(DateUtils.getDate(entity.getDate(), DateUtils.YYYY_MM_DD));

            String goods = "";
            if (!Utils.isEmpty(entity.getInGoodsGoodsVoList())) {
                for (InGoodsGoodsVo vo : entity.getInGoodsGoodsVoList()) {
                    goods += vo.getGoodsVo().getName() + "、";
                }
            }

            if (!Utils.isEmpty(goods)) {
                goods = goods.substring(0, goods.length() - 1);
            }
            excel.setGoods(goods);
            excel.setAmount(PdosUtils.getPrice(entity.getOldTotalAmt(), businessSet));
            excel.setTax(PdosUtils.getPrice(entity.getTax(), businessSet));
            excelList.add(excel);
        }

        String fileName = "进货报表按供应商按单据表" + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";
        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path + fileName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            Sheet sheet1 = new Sheet(1, 0, ExcelInGoodsAnalysisProviderOrder.class);
            sheet1.setSheetName("进货报表按供应商按单据表信息");
            writer.write(excelList, sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(fileName, new Date().getTime());
            return ResponseData.success(fileName);
        } catch (IOException e) {
            return ResponseData.fail("导出失败");
        }
    }

    @ResponseBody
    @RequiresPermissions("inGoodsAnalysis:export")
    @RequestMapping("/inGoodsAnalysis/exportOrderDetailData")
    public ResponseData exportOrderDetailData(String date,
                                              String storeId,
                                              String houseId,
                                              String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();
        List<InGoodsVo> list = inGoodsService.listVo(company.getId(),
                null,
                null,
                false,
                Utils.genLikeData(search),
                null,
                null,
                null,
                null,
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                null,
                null,
                null,
                null,
                Utils.genDate(date),
                null,
                houseId,
                null);


        List<ExcelInGoodsAnalysisOrderDetail> excelList = new ArrayList<>();
        for (InGoodsVo entity : list) {
            ExcelInGoodsAnalysisOrderDetail excel = new ExcelInGoodsAnalysisOrderDetail();
            excel.setNo(entity.getNo());
            excel.setDate(DateUtils.getDate(entity.getDate(), DateUtils.YYYY_MM_DD));

            String goods = "";
            if (!Utils.isEmpty(entity.getInGoodsGoodsVoList())) {
                for (InGoodsGoodsVo vo : entity.getInGoodsGoodsVoList()) {
                    goods += vo.getGoodsVo().getName() + "、";
                }
            }

            if (!Utils.isEmpty(goods)) {
                goods = goods.substring(0, goods.length() - 1);
            }
            excel.setGoods(goods);
            excel.setAmount(PdosUtils.getPrice(entity.getOldTotalAmt(), businessSet));
            excel.setOtherAmt(PdosUtils.getPrice(entity.getOtherAmt(), businessSet));
            excel.setTax(PdosUtils.getPrice(entity.getTax(), businessSet));
            excelList.add(excel);
        }

        String fileName = "进货报表按单据详情表" + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";
        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path + fileName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            Sheet sheet1 = new Sheet(1, 0, ExcelInGoodsAnalysisOrderDetail.class);
            sheet1.setSheetName("进货报表按单据详情表信息");
            writer.write(excelList, sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(fileName, new Date().getTime());
            return ResponseData.success(fileName);
        } catch (IOException e) {
            return ResponseData.fail("导出失败");
        }
    }
}
